﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodeTitans.JSon.Tests
{
    /// <summary>
    /// Summary description for JSonAutoSerialization
    /// </summary>
    [TestClass]
    public class JSonAutoSerialization
    {
        class JSonSampleClass : IJSonSerializable, IEquatable<JSonSampleClass>
        {
            public JSonSampleClass()
            {
                ID = Guid.NewGuid();
            }

            public JSonSampleClass(string name, int age, double salary)
            {
                ID = Guid.NewGuid();
                Name = name;
                Age = age;
                Salary = salary;
            }

            #region Properties

            public Guid ID
            { get; private set; }

            public string Name
            { get; private set; }

            public int Age
            { get; private set; }

            public double Salary
            { get; private set; }

            #endregion

            #region IJSonWritable Members

            public void Write(IJSonWriter output)
            {
                output.WriteObjectStart();
                output.WriteMember("ID", ID);
                output.WriteMember("Name", Name);
                output.WriteMember("Age", Age);
                output.WriteMember("Salary", Salary);
                output.WriteObjectEnd();
            }

            #endregion

            #region IJSonReadable Members

            public void Read(IJSonObject input)
            {
                ID = input["ID"].GuidValue;
                Name = input["Name"].StringValue;
                Age = input["Age"].Int32Value;
                Salary = input["Salary"].DoubleValue;
            }

            #endregion

            public override bool Equals(object obj)
            {
                return base.Equals(obj as JSonSampleClass);
            }

            #region IEquatable<JSonSampleClass> Members

            public bool Equals(JSonSampleClass other)
            {
                if (other != null)
                {
                    return other.ID == ID && other.Name == Name && other.Age == Age && other.Salary == Salary;
                }

                return false;
            }

            #endregion
        }

        private JSonReader reader;
        private JSonWriter writer;

        [TestInitialize]
        public void TestInit()
        {
            reader = new JSonReader();
            writer = new JSonWriter(true);
        }

        [TestCleanup]
        public void TestClean()
        {
            Console.WriteLine("Serialized JSON object:");
            Console.WriteLine(writer.ToString());
            writer.Close();
        }

        [TestMethod]
        public void TestSerializeAndDeserialize()
        {
            JSonSampleClass o1 = new JSonSampleClass("Paweł", 30, 1001.1);
            JSonSampleClass d1 = new JSonSampleClass();

            Assert.AreNotEqual(o1, d1, "Can't be equal!");

            writer.Write(o1);
            var result = reader.ReadAsJSonObject(writer.ToString());

            d1.Read(result);

            Assert.IsTrue(o1.Equals(d1), "Object is not deserialized correctly!");
        }

        [TestMethod]
        public void TestSerializeArrayWithString()
        {
            string text = "Jakiś tekst!\r\n Paweł";

            writer.WriteArrayStart();
            writer.WriteValue(text);
            writer.WriteArrayEnd();
            Assert.AreEqual(writer.ToString(), "[\r\n    \"Jakiś tekst!\\r\\n Paweł\"\r\n]", "Text is deserialized incorrectly!");

            var result = reader.Read(writer.ToString()) as object[];
            Assert.AreEqual(text, result[0], "Text after deserialization are not equal!");
        }
    }
}
